package com.onesports.intelligent.k12.polarlight.service.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.intelligent.k12.polarlight.domain.dto.operation.FtbPageOrderDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.operation.PageOrderDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.OrgPageOrderDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.market.FundsTradeBill;
import com.onesports.intelligent.k12.polarlight.domain.entity.market.Orders;
import com.onesports.intelligent.k12.polarlight.domain.vo.market.TotalDataVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.operation.CommissionPageVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.operation.FundsTradeBillPageVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.operation.FundsTradeBillTotalVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.order.OrderByIdVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.order.OrderPageVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.order.OrgOrderPageVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.student.StudentCourseVO;
import com.onesports.intelligent.k12.polarlight.mapper.market.FundsTradeBillMapper;
import com.onesports.intelligent.k12.polarlight.mapper.market.OrdersMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * 订单查询
 *
 * @author chenrl
 * @since 1.0.0 2022-03-16
 */
@Service
@RequiredArgsConstructor
public class FundsTradeBillService extends ServiceImpl<FundsTradeBillMapper, FundsTradeBill> {

    private final FundsTradeBillMapper fundsTradeBillMapper;

    /**
     * 运营端-分页查询账单信息导出
     * @param fundsTradeBillPageVOPage
     * @param ftbPageOrderDTO
     * @return
     */
    public IPage<FundsTradeBillPageVO> selectPage(Page<FundsTradeBillPageVO> fundsTradeBillPageVOPage, FtbPageOrderDTO ftbPageOrderDTO) {

        return fundsTradeBillMapper.findPage(fundsTradeBillPageVOPage, ftbPageOrderDTO);
    }
    /**
     * 运营端-分页查询账单信息带计算总和
     * @param fundsTradeBillPageVOPage
     * @param ftbPageOrderDTO
     * @return
     */
    public TotalDataVO<FundsTradeBillPageVO> selectPageAndTotal(Page<FundsTradeBillPageVO> fundsTradeBillPageVOPage, FtbPageOrderDTO ftbPageOrderDTO) {
        TotalDataVO<FundsTradeBillPageVO> fundsTradeBillPageVOTotalDataVO = new TotalDataVO<>();
        IPage<FundsTradeBillPageVO> page = fundsTradeBillMapper.findPage(fundsTradeBillPageVOPage, ftbPageOrderDTO);
        fundsTradeBillPageVOTotalDataVO.setPage(page);
        FundsTradeBillTotalVO fundsTradeBillTotalVO = Optional
                .ofNullable(fundsTradeBillMapper.findPageTotal(fundsTradeBillPageVOPage, ftbPageOrderDTO))
                .orElse(new FundsTradeBillTotalVO());

//        BigDecimal toTal = fundsTradeBillMapper.findPageTotal(fundsTradeBillPageVOPage, ftbPageOrderDTO).getToTal();
        FundsTradeBillPageVO fundsTradeBillPageVO = new FundsTradeBillPageVO();
        fundsTradeBillPageVO.setAmount(fundsTradeBillTotalVO.getToTal());
        fundsTradeBillPageVOTotalDataVO.setTotalData(fundsTradeBillPageVO);
        return fundsTradeBillPageVOTotalDataVO;
    }

    public List<FundsTradeBillPageVO> selectExport(FtbPageOrderDTO dto) {
        List<FundsTradeBillPageVO> records = fundsTradeBillMapper.findPage(new Page<>(1, Integer.MAX_VALUE), dto).getRecords();
        FundsTradeBillTotalVO pageTotal = fundsTradeBillMapper.findPageTotal(new Page<>(1, Integer.MAX_VALUE), dto);
        if(null!=pageTotal){
            BigDecimal toTal = pageTotal.getToTal();
            FundsTradeBillPageVO fundsTradeBillPageVO = new FundsTradeBillPageVO();
            fundsTradeBillPageVO.setTradeNo("合计：");
            fundsTradeBillPageVO.setAmount(toTal);
            records.add(fundsTradeBillPageVO);
        }
        if(CollectionUtils.isNotEmpty(records)){
            return records;
        }
        return null;
    }
}
